<?php
// +----------------------------------------------------------------------
// | [ api-yun.lib ] wo.pan
// +----------------------------------------------------------------------
// | Author: 北菜菜 <1440802@qq.com>
// +----------------------------------------------------------------------

namespace wopan\lib;

/**
 * Class WoPanRequest
 * @package wopan\lib
 */
class WoPanRequest
{
    const BASE_URL = "https://iotpservice.smartont.net/";
    const REQUEST_LOG_OPEN = true;

    /**
     * 获取13位的时间戳
     */
    public static function getUnixTimestamp()
    {
        list($s1, $s2) = explode(' ', microtime());
        return (int)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000);
    }
    /**
     * 获取随机数
     */
    public static function getRandom($min = 0, $max = 1)
    {
        return $min + mt_rand() / mt_getrandmax() * ($max - $min);
    }

    /**
     * 发起请求
     */
    public static function send($url, $conn, $body = [], $header = [])
    {
        // 生成签名
        $key = $conn[1]; // key
        $resTime = self::getUnixTimestamp(); // resTime
        $reqSeq = (int)(89999 * self::getRandom() + 100000); // reqSeq
        $channel = $conn[0]; // channel
        $r = "";
        $sign = md5($key . $resTime .  $reqSeq . $channel . $r);
        // 组装参数
        $data = [
            'body' => $body,
            'header' => [
                "channel" => $channel,
                "key"     => $key,
                "reqSeq"  => $reqSeq,
                "resTime" => $resTime,
                "sign"    => $sign,
                "version" => ""
            ]
        ];
        $data = json_encode($data);
        // 设置请求头
        $header[] = 'content-length: ' . strlen($data);
        $header[] = 'content-type: application/json;charset=utf-8';

        return self::curlRequest(self::BASE_URL . $url, $data, $header);
    }
    public static function curlRequest($url, $post_data = '', $header = array(), $timeout = 30, $SSL = FALSE)
    {
        // 调试
        self::REQUEST_LOG_OPEN && file_put_contents(__DIR__ . DIRECTORY_SEPARATOR . 'logs' . DIRECTORY_SEPARATOR . date('Ymd') . 'log', "[" . date('Y-m-d H:i:s') . "]\r\n" . json_encode([$url, $header, json_decode($post_data, true)]) . "\r\n", FILE_APPEND);

        $ch = curl_init();
        if ($SSL) {
            if (substr($url, 0, 8) != "https://") $url = "https://" . $url;
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        }
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        if (!empty($post_data)) {
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_DNS_USE_GLOBAL_CACHE, false);
        $response = curl_exec($ch);
        if ($error = curl_error($ch)) {
            die($error);
        }
        curl_close($ch);
        return json_decode($response, true);
    }
    public static function encryptParams($data, $key = null)
    {
        return WoPanAES::encrypt(json_encode($data), $key);
    }
    public static function decryptParams($data, $key = null)
    {
        return WoPanAES::decrypt($data, $key);
    }
}
